from django.shortcuts import render, HttpResponse, reverse, redirect
from django.views import View

from article.models import Article
from blog.models import Adv, Blog
from libs.CommonPagination import Pagination
from libs.CommonResponse import CommonReponse


def home(request, *args, **kwargs):
    # 获取所有的文章对象
    article_data = Article.objects.all()

    # 获取到当前的页数
    current_page = request.GET.get("page", 1)
    # 获取到当前数据量的总数
    all_count = article_data.count()
    # 生成分页器对象
    page_obj = Pagination(current_page=current_page, all_count=all_count, per_page_num=5)
    # 对源数据进行切割
    page_queryset = article_data[page_obj.start:page_obj.end]

    return render(request, "home.html", locals())


class AdvDetailView(View):
    def get(self, request, *args, **kwargs):
        # 【二】拿到所有的广告信息
        adv_data_list = Adv.objects.all()
        # 【一】筛选出轮播图图片
        background_img_list = adv_data_list.filter(is_background_img=True).all()
        return render(request, "adv/detail.html", locals())

    def post(self, request, *args, **kwargs):
        if request.is_ajax():
            data = request.POST
            title = data.get("adv_title")
            content = data.get("adv_content")
            mobile = data.get("adv_mobile")
            is_background_img = data.get("is_background_img")
            img = request.FILES.get("adv_img")
            if not all([title, content, mobile, is_background_img, img]):
                return CommonReponse(code=2002, errors="请补齐参数!")
            # 写入数据
            Adv.objects.create(title=title, content=content, mobile=mobile, img=img,
                               is_background_img=int(is_background_img))
            return CommonReponse(code=2000, message="广告添加成功!", next_url=reverse('adv_detail'))


def site(request, username, condition=None, params=None, *args, **kwargs):
    # print(args)
    # print(condition) #tag 4
    # print(params)#2024-03
    # 【一】获取到当前用户的用户名，根据指定的用户名筛选出指定的文章
    # 从文章表跨到个人站点表再跨到用户表 根据用户名筛选出指定的用户数据
    # blog_obj = Blog.objects.get(userinfo__username=username)
    #
    # # 【一】获取到当前用户的用户名，根据指定的用户名筛选出指定的文章
    # # 从文章表跨到个人站点表再跨到用户表 根据用户名筛选出指定的用户数据
    # # 所有的文章数据
    # article_data_all = Article.objects.filter(blog=blog_obj)

    # blog_obj = Blog.objects.get(userinfo__username=username)

    # 【一】获取到当前用户的用户名，根据指定的用户名筛选出指定的文章
    # 从文章表跨到个人站点表再跨到用户表 根据用户名筛选出指定的用户数据
    # 所有的文章数据
    article_data_all = Article.objects.filter(blog__userinfo__username=username)

    # print(condition) # category archive
    # print(params, type(params)) # 1 <class 'str'> 2022-06 <class 'str'>
    if condition and params:
        # 判断当前是文章分类下的文章
        if condition == "category":
            article_data_all = Article.objects.filter(category__pk=int(params))
        # 判断当前是文章标签下的文章
        elif condition == "tag":
            article_data_all = Article.objects.filter(tags__pk=int(params))
        # 判断当前是文章归档下的文章
        elif condition == "archive":
            year, month = params.split("-")
            article_data_all = Article.objects.filter(create_time__year=year, create_time__month=month)
        # 判断当前路径不是我们想要的路由直接重定向回个人站点页
        else:
            return redirect("site", (username))

    # 获取到当前的页数
    current_page = request.GET.get("page", 1)
    # 获取到当前数据量的总数
    all_count = article_data_all.count()
    # 生成分页器对象
    page_obj = Pagination(current_page=current_page, all_count=all_count, per_page_num=5)
    # 对源数据进行切割
    page_queryset = article_data_all[page_obj.start:page_obj.end]
    print(page_queryset)

    return render(request, "site.html", locals())


def avatar(request, *args, **kwargs):
    return render(request, "avatar.html", locals())
